package com.zeedev.flyingsaucer.beers.downloaders;

import android.app.Activity;
import android.widget.Toast;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.interfaces.ListModel;
import com.zeedev.flyingsaucer.modules.IndicatorModule.interfaces.Indicator;
import roboguice.util.Ln;
import roboguice.util.RoboAsyncTask;

public class BeersDownloader extends RoboAsyncTask {

    @Inject
    Indicator indicator;

    @Inject
    Provider<Activity> mActivity;


    @Override
    public Object call() throws Exception {

//        Debug.startMethodTracing();
//        Debug.startNativeTracing();
//        Debug.startAllocCounting();
        listModel.masterRefresh();
//        Debug.stopMethodTracing();
//        Debug.stopNativeTracing();
//        Debug.stopAllocCounting();
        return null;
    }

    private final ListModel listModel;

    @Inject
    public BeersDownloader(ListModel listModel) {
        this.listModel = listModel;
    }

    @Override
    protected void onSuccess(Object o) throws Exception {
        super.onSuccess(o);
    }

    @Override
    protected void onPreExecute() throws Exception {
        super.onPreExecute();
        indicator.setIndicatorStatus(true);

    }

    @Override
    protected void onException(Exception e) throws RuntimeException {
        super.onException(e);
        Toast.makeText(mActivity.get(),"There was an error getting your beers from the serer.", Toast.LENGTH_SHORT).show();
        Ln.w(e);
    }

    @Override
    protected void onFinally() throws RuntimeException {
        super.onFinally();

        indicator.setIndicatorStatus(false);


    }

    @Override
    protected void onInterrupted(InterruptedException e) {
        super.onInterrupted(e);
        Ln.i(listModel.getClass().getName() + ": Cancel completed.", e);
//        indicator.setIndicatorStatus(false);
    }



}
